package com.xyc.servlet;

import com.xyc.mapper.OrderMapper;
import com.xyc.pojo.Order;
import lombok.SneakyThrows;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addOrder")
public class addOrderServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("addOrder....");

        req.setCharacterEncoding("utf-8");

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        OrderMapper orderMapper = ctx.getBean("orderMapper", OrderMapper.class);
        Integer num = orderMapper.getNum();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM");

        Date date = new Date();
        Order order = new Order(num+1,date,"3.png","HUAWEI P40pro+",1,7988,100,7888);
        orderMapper.addOrder(order);

        resp.sendRedirect("buycar.jsp");
    }
}
